﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhuZhongWei.Models;

namespace ZhuZhongWei.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 所有分类
        /// </summary>
        /// <returns></returns>
        public ActionResult TypeIist()
        {
            using (zzwEntities db = new zzwEntities())
            {
                var list = db.airports.Select(p => new
                { aid = p.ald, airportName = p.airportName }).ToList();
                return Json(list, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 数据表格
        /// </summary>
        /// <returns></returns>

        public ActionResult GetPageIist(int page, int limit, string flightNo, string meals, string departureAirport)
        {
            using (zzwEntities db = new zzwEntities())
            {
                var dataList = db.flights.Include("airport").AsEnumerable();
                if (!String.IsNullOrEmpty(flightNo))
                {
                    dataList = dataList.Where(p => p.flightNo.Contains(flightNo));
                }
                if (!String.IsNullOrEmpty(meals))
                {
                    dataList = dataList.Where(p => p.meals == meals);
                }
                if (!String.IsNullOrEmpty(departureAirport))
                {
                    dataList = dataList.Where(p => p.airport.ald == Convert.ToInt32(departureAirport));
                }
                //筛选页面符合的数据
                var list = dataList.Select(p => new
                {
                    fId = p.fId,
                    flightNo = p.flightNo,
                    departureAirport = p.departureAirport,
                    departureAirportType = p.airport.airportName,
                    arriveAirport = p.arriveAirport,
                    arriveAirportType = p.airport.airportName,
                    departureTime = p.departureTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    arriveTime = p.arriveTime.ToString("yyyy/MM/dd HH:mm:ss"),
                    price = p.price,
                    meals = p.meals
                });
                var count = list.Count();
                return Json(new { count = count, data = list.OrderByDescending(p => p.fId).Skip((page - 1) * limit).Take(limit).ToList() });
            }
        }

        /// <summary>
        /// 出发机场分组
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTypeList()
        {
            using (zzwEntities db = new zzwEntities())
            {
                var list = db.airports.Select(p => new { aId = p.ald, airportName = p.airportName }).ToList();
                return Json(list, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult PostAdd(flight flight)
        {
            using (zzwEntities db = new zzwEntities())
            {
                db.flights.Add(flight);
                int n = db.SaveChanges();
                if (n > 0)
                {
                    return Json(new { code = true, msg = "添加成功" });
                }
                return Json(new { code = false, msg = "添加失败" });
            }
        }
    }
}